<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>22.2. Zend_Layout Quick Start</title>
<link rel="stylesheet" href="dbstyle.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0">
<link rel="start" href="index.html" title="Programmer's Reference Guide">
<link rel="up" href="zend.layout.html" title="Chapter 22. Zend_Layout">
<link rel="prev" href="zend.layout.html" title="Chapter 22. Zend_Layout">
<link rel="next" href="zend.layout.options.html" title="22.3. Zend_Layout Configuration Options">
<link rel="chapter" href="introduction.html" title="Chapter 1. Introduction to Zend Framework">
<link rel="chapter" href="zend.acl.html" title="Chapter 2. Zend_Acl">
<link rel="chapter" href="zend.auth.html" title="Chapter 3. Zend_Auth">
<link rel="chapter" href="zend.cache.html" title="Chapter 4. Zend_Cache">
<link rel="chapter" href="zend.config.html" title="Chapter 5. Zend_Config">
<link rel="chapter" href="zend.console.getopt.html" title="Chapter 6. Zend_Console_Getopt">
<link rel="chapter" href="zend.controller.html" title="Chapter 7. Zend_Controller">
<link rel="chapter" href="zend.currency.html" title="Chapter 8. Zend_Currency">
<link rel="chapter" href="zend.date.html" title="Chapter 9. Zend_Date">
<link rel="chapter" href="zend.db.html" title="Chapter 10. Zend_Db">
<link rel="chapter" href="zend.debug.html" title="Chapter 11. Zend_Debug">
<link rel="chapter" href="zend.dojo.html" title="Chapter 12. Zend_Dojo">
<link rel="chapter" href="zend.dom.html" title="Chapter 13. Zend_Dom">
<link rel="chapter" href="zend.exception.html" title="Chapter 14. Zend_Exception">
<link rel="chapter" href="zend.feed.html" title="Chapter 15. Zend_Feed">
<link rel="chapter" href="zend.filter.html" title="Chapter 16. Zend_Filter">
<link rel="chapter" href="zend.form.html" title="Chapter 17. Zend_Form">
<link rel="chapter" href="zend.gdata.html" title="Chapter 18. Zend_Gdata">
<link rel="chapter" href="zend.http.html" title="Chapter 19. Zend_Http">
<link rel="chapter" href="zend.infocard.html" title="Chapter 20. Zend_InfoCard">
<link rel="chapter" href="zend.json.html" title="Chapter 21. Zend_Json">
<link rel="chapter" href="zend.layout.html" title="Chapter 22. Zend_Layout">
<link rel="chapter" href="zend.ldap.html" title="Chapter 23. Zend_Ldap">
<link rel="chapter" href="zend.loader.html" title="Chapter 24. Zend_Loader">
<link rel="chapter" href="zend.locale.html" title="Chapter 25. Zend_Locale">
<link rel="chapter" href="zend.log.html" title="Chapter 26. Zend_Log">
<link rel="chapter" href="zend.mail.html" title="Chapter 27. Zend_Mail">
<link rel="chapter" href="zend.measure.html" title="Chapter 28. Zend_Measure">
<link rel="chapter" href="zend.memory.html" title="Chapter 29. Zend_Memory">
<link rel="chapter" href="zend.mime.html" title="Chapter 30. Zend_Mime">
<link rel="chapter" href="zend.openid.html" title="Chapter 31. Zend_OpenId">
<link rel="chapter" href="zend.paginator.html" title="Chapter 32. Zend_Paginator">
<link rel="chapter" href="zend.pdf.html" title="Chapter 33. Zend_Pdf">
<link rel="chapter" href="zend.registry.html" title="Chapter 34. Zend_Registry">
<link rel="chapter" href="zend.rest.html" title="Chapter 35. Zend_Rest">
<link rel="chapter" href="zend.search.lucene.html" title="Chapter 36. Zend_Search_Lucene">
<link rel="chapter" href="zend.server.html" title="Chapter 37. Zend_Server">
<link rel="chapter" href="zend.service.html" title="Chapter 38. Zend_Service">
<link rel="chapter" href="zend.session.html" title="Chapter 39. Zend_Session">
<link rel="chapter" href="zend.soap.html" title="Chapter 40. Zend_Soap">
<link rel="chapter" href="zend.test.html" title="Chapter 41. Zend_Test">
<link rel="chapter" href="zend.text.html" title="Chapter 42. Zend_Text">
<link rel="chapter" href="zend.timesync.html" title="Chapter 43. Zend_TimeSync">
<link rel="chapter" href="zend.translate.html" title="Chapter 44. Zend_Translate">
<link rel="chapter" href="zend.uri.html" title="Chapter 45. Zend_Uri">
<link rel="chapter" href="zend.validate.html" title="Chapter 46. Zend_Validate">
<link rel="chapter" href="zend.version.html" title="Chapter 47. Zend_Version">
<link rel="chapter" href="zend.view.html" title="Chapter 48. Zend_View">
<link rel="chapter" href="zend.xmlrpc.html" title="Chapter 49. Zend_XmlRpc">
<link rel="appendix" href="requirements.html" title="Appendix A. Zend Framework Requirements">
<link rel="appendix" href="coding-standard.html" title="Appendix B. Zend Framework Coding Standard for PHP">
<link rel="appendix" href="copyrights.html" title="Appendix C. Copyright Information">
<link rel="index" href="the.index.html" title="Index">
<link rel="subsection" href="zend.layout.quickstart.html#zend.layout.quickstart.layouts" title="22.2.1. Layout scripts">
<link rel="subsection" href="zend.layout.quickstart.html#zend.layout.quickstart.mvc" title="22.2.2. Using Zend_Layout with the Zend Framework MVC">
<link rel="subsection" href="zend.layout.quickstart.html#zend.layout.quickstart.standalone" title="22.2.3. Using Zend_Layout as a Standalone Component">
<link rel="subsection" href="zend.layout.quickstart.html#zend.layout.quickstart.example" title="22.2.4. Sample Layout">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader"><table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">22.2. Zend_Layout Quick Start</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="zend.layout.html">Prev</a> </td>
<th width="60%" align="center">Chapter 22. Zend_Layout</th>
<td width="20%" align="right"> <a accesskey="n" href="zend.layout.options.html">Next</a>
</td>
</tr>
</table></div>
<div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="zend.layout.quickstart"></a>22.2. Zend_Layout Quick Start</h2></div></div></div>
<p>
        There are two primary use cases for <code class="code">Zend_Layout</code>: with the
        Zend Framework MVC, and without.
    </p>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.layout.quickstart.layouts"></a>22.2.1. Layout scripts</h3></div></div></div>
<p>
            In both cases, however, you'll need to create a layout script.
            Layout scripts simply utilize Zend_View (or whatever view
            implementation you are using). Layout variables are registered with
            a <code class="code">Zend_Layout</code> <a href="zend.view.helpers.html#zend.view.helpers.initial.placeholder" title="48.4.1.3. Placeholder Helper">placeholder</a>, 
            and may be accessed via the placeholder helper or by fetching them
            as object properties of the layout object via the layout helper.
        </p>
<p>
            As an example:
        </p>
<pre class="programlisting">
&lt;!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
    &lt;title&gt;My Site&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php 
    // fetch 'content' key using layout helper:
    echo $this-&gt;layout()-&gt;content; 

    // fetch 'foo' key using placeholder helper:
    echo $this-&gt;placeholder('Zend_Layout')-&gt;foo;

    // fetch layout object and retrieve various keys from it:
    $layout = $this-&gt;layout();
    echo $layout-&gt;bar;
    echo $layout-&gt;baz;
?&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>
            Because <code class="code">Zend_Layout</code> utilizes <code class="code">Zend_View</code> for
            rendering, you can also use any view helpers registered, and also
            have access to any previously assigned view variables. Particularly
            useful are the various <a href="zend.view.helpers.html#zend.view.helpers.initial.placeholder" title="48.4.1.3. Placeholder Helper">placeholder
                helpers</a>, as they allow you to
            retrieve content for areas such as the &lt;head&gt; section,
            navigation, etc.:
        </p>
<pre class="programlisting">
&lt;!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
    &lt;?= $this-&gt;headTitle() ?&gt;
    &lt;?= $this-&gt;headScript() ?&gt;
    &lt;?= $this-&gt;headStyle() ?&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;?= $this-&gt;render('header.phtml') ?&gt;

    &lt;div id="nav"&gt;&lt;?= $this-&gt;placeholder('nav') ?&gt;&lt;/div&gt;

    &lt;div id="content"&gt;&lt;?= $this-&gt;layout()-&gt;content ?&gt;&lt;/div&gt;

    &lt;?= $this-&gt;render('footer.phtml') ?&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.layout.quickstart.mvc"></a>22.2.2. Using Zend_Layout with the Zend Framework MVC</h3></div></div></div>
<p>
            <code class="code">Zend_Controller</code> offers a rich set of functionality for
            extension via its <a href="zend.controller.plugins.html" title="7.10. Plugins">front
            controller plugins</a> and <a href="zend.controller.actionhelpers.html" title="7.8. Action Helpers">action controller
            helpers</a>. <code class="code">Zend_View</code> also has <a href="zend.view.helpers.html" title="48.4. View Helpers">helpers</a>. <code class="code">Zend_Layout</code>
            takes advantage of these various extension points when used with the
            MVC components.
        </p>
<p>
            <code class="code">Zend_Layout::startMvc()</code> creates an instance of
            <code class="code">Zend_Layout</code> with any optional configuration you provide
            it. It then registers a front controller plugin that renders the
            layout with any application content once the dispatch loop is done,
            and registers an action helper to allow access to the layout object
            from your action controllers. Additionally, you may at any time grab
            the layout instance from within a view script using the
            <code class="code">layout</code> view helper.
        </p>
<p>
            First, let's look at how to initialize Zend_Layout for use with the
            MVC:
        </p>
<pre class="programlisting">&lt;?php
// In your bootstrap:
Zend_Layout::startMvc();
?&gt;</pre>
<p>
            <code class="code">startMvc()</code> can take an optional array of options or
            <code class="code">Zend_Config</code> object to customize the instance; these
            options are detailed in <a href="zend.layout.options.html" title="22.3. Zend_Layout Configuration Options">Section 22.3, “Zend_Layout Configuration Options”</a>.
        </p>
<p>
            In an action controller, you may then access the layout instance as
            an action helper:
        </p>
<pre class="programlisting">&lt;?php
class FooController extends Zend_Controller_Action
{
    public function barAction()
    {
        // disable layouts for this action:
        $this-&gt;_helper-&gt;layout-&gt;disableLayout();
    }

    public function bazAction()
    {
        // use different layout script with this action:
        $this-&gt;_helper-&gt;layout-&gt;setLayout('foobaz');
    };
}
?&gt;</pre>
<p>
            In your view scripts, you can then access the layout object via the
            <code class="code">layout</code> view helper. This view helper is slightly
            different than others in that it takes no arguments, and returns an
            object instead of a string value. This allows you to immediately
            call methods on the layout object:
        </p>
<pre class="programlisting">
&lt;?php $this-&gt;layout()-&gt;setLayout('foo'); // set alternate layout ?&gt;
</pre>
<p>
            At any time, you can fetch the <code class="code">Zend_Layout</code> instance
            registered with the MVC via the <code class="code">getMvcInstance()</code> static
            method:
        </p>
<pre class="programlisting">&lt;?php
// Returns null if startMvc() has not first been called
$layout = Zend_Layout::getMvcInstance(); 
?&gt;</pre>
<p>
            Finally, <code class="code">Zend_Layout</code>'s front controller plugin has one
            important feature in addition to rendering the layout: it retrieves
            all named segments from the response object and assigns them as
            layout variables, assigning the 'default' segment to the variable
            'content'. This allows you to access your application content and
            render it in your view scripts.
        </p>
<p>
            As an example, let's say your code first hits
            <code class="code">FooController::indexAction()</code>, which renders some
            content to the default response segment, and then forwards to
            <code class="code">NavController::menuAction()</code>, which renders content to
            the 'nav' response segment. Finally, you forward to
            <code class="code">CommentController::fetchAction()</code> and fetch some
            comments, but render those to the default response segment as well
            (which appends content to that segment). Your view script could then
            render each separately:
        </p>
<pre class="programlisting">
&lt;body&gt;
    &lt;!-- renders /nav/menu --&gt;
    &lt;div id="nav"&gt;&lt;?= $this-&gt;layout()-&gt;nav ?&gt;&lt;/div&gt;

    &lt;!-- renders /foo/index + /comment/fetch --&gt;
    &lt;div id="content"&gt;&lt;?= $this-&gt;layout()-&gt;content ?&gt;&lt;/div&gt;
&lt;/body&gt;
</pre>
<p>
            This feature is particularly useful when used in conjunction with
            the ActionStack <a href="zend.controller.actionhelpers.html#zend.controller.actionhelpers.actionstack" title="7.8.4.1. ActionStack">action
            helper</a> and <a href="zend.controller.plugins.html#zend.controller.plugins.standard.actionstack" title="7.10.5.1. ActionStack">plugin</a>,
            which you can use to setup a stack of actions through
            which to loop, and thus create widgetized pages.
        </p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.layout.quickstart.standalone"></a>22.2.3. Using Zend_Layout as a Standalone Component</h3></div></div></div>
<p>
            As a standalone component, Zend_Layout does not offer nearly as many
            features or as much convenience as when used with the MVC. However,
            it still has two chief benefits:
        </p>
<div class="itemizedlist"><ul type="disc">
<li><p>
                    Scoping of layout variables.
            </p></li>
<li><p>
                    Isolation of layout view script from other view scripts.
            </p></li>
</ul></div>
<p>
            When used as a standalone component, simply instantiate the layout
            object, use the various accessors to set state, set variables as
            object properties, and render the layout:
        </p>
<pre class="programlisting">&lt;?php
$layout = new Zend_Layout();

// Set a layout script path:
$layout-&gt;setLayoutPath('/path/to/layouts');

// set some variables:
$layout-&gt;content = $content;
$layout-&gt;nav     = $nav;

// choose a different layout script:
$layout-&gt;setLayout('foo');

// render final layout
echo $layout-&gt;render();
?&gt;</pre>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.layout.quickstart.example"></a>22.2.4. Sample Layout</h3></div></div></div>
<p>
            Sometimes a picture is worth a thousand words. The following is a
            sample layout script showing how it might all come together.
        </p>
<p>
            <img src="figures/zend.layout.quickstart.example.png" align="middle">
        </p>
<p>
            The actual order of elements may vary, depending on the CSS you've
            setup; for instance, if you're using absolute positioning, you may
            be able to have the navigation displayed later in the document, but
            still show up at the top; the same could be said for the sidebar or
            header. The actual mechanics of pulling the content remain the same,
            however.
        </p>
</div>
</div>
<div class="navfooter"><table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="zend.layout.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="zend.layout.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="zend.layout.options.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Chapter 22. Zend_Layout </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> 22.3. Zend_Layout Configuration Options</td>
</tr>
</table></div>
<div class="revinfo"></div>
</body>
</html>
